/**
 * 学生 选择器（单选、多选）
 */
var StudentSelector = {

	/**
	 * callbackOrConf 回调函数或配置选项，若为map类型，则表示为配置选项 isSingle 是否单选
	 */
	getView : function(callbackOrConf, isSingle, conf, selval) {
		// 单选
		if (typeof (callbackOrConf) == 'object') {
			this.scope = callbackOrConf.scope || this;
			this.callback = callbackOrConf.callback;
		} else {
			this.scope = this;
			this.callback = callbackOrConf;
		}
		this.isSingle = (isSingle != null) ? isSingle : true;
		this.conf = conf || {};

		var panel = this.initPanel(isSingle);

		this.win = new Ext.Window({
			title : this.conf.title || '选择学生',
			iconCls : 'menu-appuser',
			width : 700,
			height : 400,
			layout : 'fit',
			border : false,
			maximizable : true,
			modal : true,
			items : [ panel ],
			buttonAlign : 'center',
			buttons : [ {
				text : '确认',
				iconCls : 'btn-ok',
				scope : this,
				handler : this.submit
			}, {
				text : '关闭',
				iconCls : 'btn-cancel',
				scope : this,
				handler : this.close
			} ]
		});
		return this.win;
	},

	/**
	 * 组件初始化 isSingle：是否单选,默认单选
	 */
	initPanel : function(isSingle) {
		this.searchPanel = new KE.SearchPanel({
			region : 'north',
			items : [ {
				text : '姓名'
			}, {
				width : 150,
				name : 'Q_LIKES_fullname',
				xtype : "textfield"
			}, {
				text : '学号'
			}, {
				width : 150,
				name : 'Q_LIKES_username',
				xtype : "textfield"
			}, {
				xtype : 'button',
				text : '查询',
				scope : this,
				iconCls : 'btn-search',
				handler : this.search
			}, {
				xtype : 'button',
				text : '清空',
				scope : this,
				iconCls : 'reset',
				handler : this.reset
			} ]
		});
		// ///////////////gridPanel[全部列表] start///////////////////
		this.gridPanel = new KE.GridPanel({
			title : '学生列表',
			region : 'center',
			pageSize : 100,
			isShowTbar : false,
			singleSelect : isSingle,
			autoWidth : true,
			url : __ctxPath + "/byxt/listStudentYuXuan.do",
			baseParams : this.conf.params || {},
			fields : ['userId', 'fullname', 'username', {
						name : 'department.depName',
						mapping : 'department.depName'
					}],
			columns : [ {
				dataIndex : 'userId',
				hidden : true
			}, {
				header : '姓名',
				dataIndex : 'fullname'
			}, {
				header : '学号',
				dataIndex : 'username'
			}, {
				header : '班级',
				dataIndex : 'department.depName'
			} ]
		});
		// ////////////////////gridPanel[全部列表] end///////////////////////

		// ////////////////////selectedUserGrid[已选列表]
		var me = this;
		this.selectedUserGrid = new KE.GridPanel({
			title : '已选学生',
			border : false,
			isShowTbar : false,
			showPaging : false,
			store : new Ext.data.ArrayStore({
				fields : ['userId', 'fullname', 'username']
			}),
			columns : [{
				header : '姓名',
				dataIndex : 'fullname'
			}, {
				header : '学号',
				dataIndex : 'username'
			} ]
		});
		this.selectedUserGrid.on('rowdblclick', function(grid, rec) {
			var grid = me.selectedUserGrid;
			var store = grid.getStore();
			var rows = grid.getSelectionModel().getSelection();
			for (var i = 0; i < rows.length; i++) {
				store.remove(rows[i]);
			}
		});
		// ///////////////////selectedUserGrid[已选列表] end //////////////
		this.gridPanel.on('rowdblclick', function(grid, rec) {
			var selGrid = me.selectedUserGrid;
			var selStore = selGrid.getStore();
			var rows = me.gridPanel.getSelectionModel().getSelection();
			for (var i = 0; i < rows.length; i++) {
				var id = rows[i].data.userId;
				var name = rows[i].data.fullname;
				var username = rows[i].data.username;
				var isExist = false;
				// 查找是否存在该记录
				for (var j = 0; j < selStore.getCount(); j++) {
					if (selStore.getAt(j).data.userId == id) {
						isExist = true;
						break;
					}
				}
				if (!isExist) {
					var newData = {
						userId : id,
						fullname : name,
						username : username
					};
					var rec = selStore.getModel().create(newData);
					selStore.insert(selStore.getCount(), rec);
				}
			}
		});

		// ///////////////////selectedPanel[多选面板] start/////////////////////
		// 多选添加的面板
		this.selectedPanel = new Ext.Panel({
			layout : 'border',
			region : 'east',
			width : 320,
			border : false,
			items : [ new Ext.Panel({
				region : 'west',
				width : 50,
				layout : {
					type : 'vbox',
					pack : 'center',
					align : 'stretch'
				},
				defaults : {
					margin : '0 5 10 5',
					xtype : 'button'
				},
				// defaultType : 'button',
				items : [ {
					iconCls : 'add-all',
					text : '',
					scope : this,
					handler : this.addAll
				}, {
					iconCls : 'rem-all',
					text : '',
					scope : this,
					handler : this.removeAll
				} ]
			}), {
				region : 'center',
				layout : 'fit',
				items : [ this.selectedUserGrid ]
			} ]
		}); // selectedPanel
		// /////////////////////selectedPanel end//////////////////////////////

		var panel = new Ext.Panel({
			id : 'contactPanel',
			layout : 'border',
			anchor : '96%,96%',
			items : [ this.searchPanel, this.gridPanel ]
		}); // end of this contactPanel
		// 添加：多选面板
		if (isSingle != null && isSingle == false) {
			panel.add(this.selectedPanel);
			panel.doLayout();
		}
		return panel;
	}, // init

	// //////////////###方法###///////////////////////

	// 添加所有
	addAll : function() {
		var gridPanel = this.gridPanel;
		var selGrid = this.selectedUserGrid;
		var selStore = selGrid.getStore();
		var rows = gridPanel.getSelectionModel().getSelection();
		for (var i = 0; i < rows.length; i++) {
			var id = rows[i].data.userId;
			var sno = rows[i].data.username;
			var name = rows[i].data.fullname;
			var isExist = false;
			// 查找是否存在该记录
			for (var j = 0; j < selStore.getCount(); j++) {
				if (selStore.getAt(j).data.userId == id) {
					isExist = true;
					break;
				}
			}
			if (!isExist) {
				var newData = {
					userId : id,
					username : sno,
					fullname : name
				};
				var rec = selStore.getModel().create(newData);
				selStore.insert(selStore.getCount(), rec);
			}
		}
		selGrid.getView().refresh();
	},

	// 移除所有
	removeAll : function() {
		var selGrid = this.selectedUserGrid;
		var rows = selGrid.getSelectionModel().getSelection();
		var selStore = selGrid.getStore();
		for (var i = 0; i < rows.length; i++) {
			selStore.remove(rows[i]);
		}
	},

	/**
	 * 确定，提交
	 * 
	 * @param isSingle
	 *            是否单选
	 * @param callback
	 *            回传函数
	 */
	submit : function() {
		var ids = '';
		var names = '';
		if (this.isSingle == null || this.isSingle) {// 选择单个用户
			var grid = this.gridPanel;
			var rows = grid.getSelectionModel().getSelection();

			for (var i = 0; i < rows.length; i++) {
				if (i > 0) {
					ids += ',';
					names += '，';
				}
				ids += rows[i].data.userId;
				names += rows[i].data.fullname;
			}
		} else {
			var selStore = this.selectedUserGrid.getStore();
			for (var i = 0; i < selStore.getCount(); i++) {
				if (i > 0) {
					ids += ',';
					names += '，';
				}
				ids += selStore.getAt(i).data.userId;
				names += selStore.getAt(i).data.fullname;
			}
		}

		if (this.callback != null) {
			this.callback.call(this.scope, ids, names);
		}
		this.win.close();
	},

	/**
	 * 关闭当前窗口
	 */
	close : function() {
		this.win.close();
	},
	// 重置查询表单
	reset : function() {
		this.searchPanel.getForm().reset();
	},
	// 按条件搜索
	search : function() {
		$search({
			searchPanel : this.searchPanel,
			gridPanel : this.gridPanel
		});
	}
};
